package com.aistar.hotel.service.impl;


import com.aistar.common.util.IdWorker;
import com.aistar.hotel.entity.Hotel;
import com.aistar.hotel.entity.HotelComment;
import com.aistar.hotel.mapper.HotelCommentMapper;
import com.aistar.hotel.service.HotelCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Created by wubin on 2019/6/23.
 */
@Service
@Transactional
public class HotelCommentServiceImpl implements HotelCommentService {
    @Autowired
    private HotelCommentMapper hotelCommentMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<HotelComment> findAll(String commenttext) {
        List<HotelComment> comments= (List<HotelComment>) redisTemplate.opsForValue().get("comment_all");
        if (null==comments){
            comments=hotelCommentMapper.findAll("");
            redisTemplate.opsForValue().set("commnt_all",comments);
        }
        return comments;
    }

    @Override
    public HotelComment findById(String commentid) {
        HotelComment comment = (HotelComment) redisTemplate.opsForValue().get("comment_"+commentid);
        if (null==comment){
            comment = hotelCommentMapper.findById(commentid);
            redisTemplate.opsForValue().set("comment_"+commentid,comment);
        }


        return comment;
    }

    @Override
    public void save(HotelComment hotelComment) {

        hotelCommentMapper.save(hotelComment);
    }

    @Override
    public void delete(String commentid) {
    hotelCommentMapper.delete(commentid);
    }

    @Override
    public void update(HotelComment hotelComment) {

        hotelCommentMapper.update(hotelComment);
    }

    @Override
    public PageInfo<HotelComment> getPageInfo(String commenttext, Integer pageNow, Integer pageSize) {
      //  pageNow是指当前的页数，pageSize是指每页显示的条数，在这里进行了判断，分别判断了当这两个参数都为null的时候，给与的默认值
        PageHelper.startPage(pageNow==null?1:pageNow,pageSize==null?3:pageSize);//第二页,每页显示三条


        //需要分页的数据 - 带条件的
        //如果没有输入用户名 - 那么就查询所有的，这里也进行了参数的判断，如果username不存在那么就给与空字符串
//        也就是默认查询所有的，如果有那么就查询所要的，并且进行去空格的操做
        List<HotelComment> commentList = hotelCommentMapper.findAll(commenttext==null?"":commenttext.trim());

        PageInfo<HotelComment> pageInfo = new PageInfo<>(commentList);//将查询的对象放入发哦分页的插件对象中



        return pageInfo;
        //返回分页的插件，在测试时，是要拿出分页对象中的数据(user的集合)pageInfo.getlist
    }
}
